package com.badibadi.fragment;

import java.text.ParseException;
import java.text.SimpleDateFormat;
import java.util.ArrayList;
import java.util.Calendar;
import java.util.Date;
import java.util.HashMap;
import java.util.Iterator;
import java.util.List;
import java.util.Map;
import java.util.Set;

import org.json.JSONException;

import com.badibadi.activity.CalendarActivity;
import com.badibadi.activity.CalendarBetweenActivity;
import com.badibadi.activity.CalendarMyActivityActivity;
import com.badibadi.activity.Data_During_Activity;
import com.badibadi.activity.Data_Me_Activity;
import com.badibadi.infos.Activity_Overview_Model;
import com.badibadi.infos.Results;
import com.badibadi.mytools.CalendarTools;
import com.badibadi.mytools.Constants;
import com.badibadi.mytools.DisplayUtil;
import com.badibadi.mytools.JSONUtils;
import com.badibadi.mytools.MyThreadTool;
import com.badibadi.mytools.Utils;
import com.badibadi.uniclubber.R;
import com.google.zxing.common.StringUtils;
import com.libjph.util.StringUtil;

import android.content.Intent;
import android.graphics.Color;
import android.graphics.drawable.ColorDrawable;
import android.graphics.drawable.Drawable;
import android.graphics.drawable.GradientDrawable.Orientation;
import android.os.Bundle;
import android.os.Handler;
import android.os.Message;
import android.support.v4.app.Fragment;
import android.support.v4.app.FragmentTransaction;
import android.support.v4.view.PagerAdapter;
import android.support.v4.view.ViewPager;
import android.util.Log;
import android.view.Gravity;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.view.View.OnClickListener;
import android.view.ViewGroup.LayoutParams;
import android.widget.AdapterView;
import android.widget.AdapterView.OnItemClickListener;
import android.widget.AdapterView.OnItemLongClickListener;
import android.widget.BaseAdapter;
import android.widget.Button;
import android.widget.GridView;
import android.widget.ImageView;
import android.widget.LinearLayout;
import android.widget.TextView;
import android.widget.Toast;

//Intent 
public class CalendarFragment extends BaseFragment implements OnClickListener {

	/**
	 * 6*7格=42
	 */
	private int zonggeshu = 42;
	// private ViewPager viewPager;
	private GridView mGridView;

	private String TAG = "CalendarFragment";
	/** GridView列数 */
	private final int COLUMN_NUMBER = 7;
	/** GridView每个子类的高度 */
	private int HEIGHT_CHILDHEIGHT = 47;
	/** 月 */
	private int month;
	/** 年 */
	private int year;
	/** 是否为瑞年 */
	private boolean IsLeapYear;
	/** 当前月的前一个月的天数 */
	private int before_month_day;
	/** 保存当前月的天数 */
	private int month_day;
	/** 保存下一个月的天数 */
	private int after_month_day;
	/** 接收ViewPager的layout */
	private LinearLayout import_layout;
	/** 分为三个阶段，第一个阶段是上个月的，第二个阶段是本月的，第三个阶段是下月的 */
	private int jieduan = 0;
	private List<Integer> mlist;
	private CalendarTools mcalendarTools;
	private TextView tv_Year, tv_Month;

	private CalendarGridViewAdapter calendaradapter;
	private DisplayUtil displayUtil;

	private Integer color[];
	private int dip5_px;

	static CalendarFragment calendarFragment;

	private Button last_month, next_month;
	/**
	 * 往日里中加内容的监听器
	 */
	private CalendarListener calendarListener;
	/**
	 * 网日里中的Item项目设置侦听的监听器
	 */
	private CalendarGridViewListener calendarGridViewListener;

	private Map<String, Object> map;

	private int temp_year = 0, temp_month = 0;

	private boolean is_betweenActivity = false;// 用于判断是否是期间内的活动
	private Results results;
	private List<Activity_Overview_Model> overview_Models_starttt;
	private List<Activity_Overview_Model> models, models_1;
	private int temp_num;// 如果日历显示在当前月份则为0，否则上一月则-1，下一月则+1
	private Calendar calendar_temp;
	private String uid;// 查看者id
	// private String fid;// 登陆者id
	private String cid = null;// 俱乐部id
	private int StartYY, StartMM, StartDD;
	private int EndYY, EndMM, EndDD;

	private Handler handler = new Handler() {
		@Override
		public void handleMessage(Message msg) {
			switch (msg.what) {
			case 3:
				try {
					Utils.ExitPrgress(getActivity());
					if (!overview_Models_starttt.isEmpty()) {
//						   
//								+ overview_Models_starttt.toString());
						for (int i = 0; i < overview_Models_starttt.size(); i++) {//
							// 判断是访问用户是参与者还是发起者（a_uid,uid均为活动发起者id,需判断哪个不为空）
							if (overview_Models_starttt.get(i).getA_uid() != null) {
								if (uid.equals(overview_Models_starttt.get(i)
										.getA_uid())) {
									models.add(overview_Models_starttt.get(i));// 发起者
								} else {

								}
								models_1.add(overview_Models_starttt.get(i));// 参与者
							} else {
								if (uid.equals(overview_Models_starttt.get(i)
										.getUid())) {
									models.add(overview_Models_starttt.get(i));// 发起者
								} else {

								}
								models_1.add(overview_Models_starttt.get(i));// 参与者
							}
						}
//						   
//								+ models_1.size());
					} else {
//						   
					}
					setOnCalendarOnClistener(new CalendarListener() {

						@Override
						public boolean AddStartButton(int Year, int Month,
								int Day) {
							// TODO Auto-generated method stub
							return ShowStartButton(Year, Month, Day, StartYY,
									StartMM, StartDD);
						}

						@Override
						public boolean AddEndButton(int Year, int Month, int Day) {
							// TODO Auto-generated method stub
							return ShowEndButton(Year, Month, Day, EndYY,
									EndMM, EndDD);
						}

						@Override
						public String AddActivity(int Year, int Month, int Day) {
							// TODO Auto-generated method stub
							if (Constants.is_initiator) {
								return ShowActivity(Year, Month, Day, models);
							} else {
								return ShowActivity(Year, Month, Day, models_1);
							}
						}
					});
					calendaradapter.notifyDataSetChanged();
				} catch (Exception e) {
					// TODO: handle exception
				}
				break;
			default:
				break;
			}
		}
	};

	@Override
	public void onCreate(Bundle savedInstanceState) {
		// TODO Auto-generated method stub
		super.onCreate(savedInstanceState);
		map = new HashMap<String, Object>();
	}

	@Override
	public View onCreateView(LayoutInflater inflater, ViewGroup container,
			Bundle savedInstanceState) {
		// TODO Auto-generated method stub
		View view = inflater.inflate(R.layout.fragment_calender_import_layout,
				null);
		// viewPager = new ViewPager(getActivity());
		try {
			uid = getArguments().getString("uid");
			// fid = getArguments().getString("fid", "-1");
			cid = getArguments().getString("cid");
		} catch (Exception e) {
			// TODO: handle exception
		}
		import_layout = (LinearLayout) view
				.findViewById(R.id.fragment_calender_import_layout);
		try {
			temp_year = getArguments().getInt("year");
			temp_month = getArguments().getInt("month");
		} catch (Exception e) {
			// TODO: handle exception
			temp_month = 0;
			temp_year = 0;
//			   
		}

		overview_Models_starttt = new ArrayList<Activity_Overview_Model>();
		models = new ArrayList<Activity_Overview_Model>();
		models_1 = new ArrayList<Activity_Overview_Model>();
		if (is_betweenActivity) {

		} else {
			temp_num = 0;
			setOnCalendarItemClistener(new CalendarGridViewListener() {

				@Override
				public void ItemListener(AdapterView<?> arg0, View view,
						int position, long arg3,
						CalendarGridViewAdapter calendaradapter) {
					// TODO Auto-generated method stub
					if (backdd(Constants.temp_map,
							calendaradapter.getYearMonthDay(position)).size() > 0) {
						Intent intent = new Intent(getActivity(),
								Data_Me_Activity.class);
						// intent.setFlags(Intent.FLAG_ACTIVITY_NEW_TASK);
						intent.putExtra("日期",
								calendaradapter.getYearMonthDay(position));
//						   
//								+ " " + uid);
						intent.putExtra("uid", uid);
						if (cid != null) {
							intent.putExtra("cid", cid);
						}
						intent.putExtra("fid", Utils.getUid(getActivity()));
						startActivity(intent);
					} else {

					}
				}
			});
		}
//		   
		init(view);
		initData(temp_year, temp_month);
		initGridView();

		return view;
	}

	/**
	 * 匹配当前时间是否有活动
	 * 
	 * @param map匹配的map
	 * @param time传入需要匹配的时间
	 */
	public List<Integer> backdd(Map<String, Object> map, String time) {
		Set<Map.Entry<String, Object>> set = map.entrySet();
		// boolean jud = false;
		List<Integer> integers = new ArrayList<Integer>();
		for (Iterator<Map.Entry<String, Object>> it = set.iterator(); it
				.hasNext();) {
			Map.Entry<String, Object> entry = (Map.Entry<String, Object>) it
					.next();
			//    (entry.getKey() + "--->" +
			// entry.getValue());
			if (Utils.getStringMillis(time) == Utils.getStringMillis(entry
					.getKey())) {
				// jud = true;
				integers = (List<Integer>) entry.getValue();
			}
		}
		Constants.temp_num = integers;
		return integers;
	}

	protected boolean ShowStartButton(int Y, int M, int D, int YY, int MM,
			int DD) {
		if (YY == Y && MM == M && DD == D) {
			return true;
		} else {
			return false;
		}
	}

	protected boolean ShowEndButton(int Y, int M, int D, int YY, int MM, int DD) {
		if (YY == Y && MM == M && DD == D) {
			return true;
		} else {
			return false;
		}
	}

	public String ShowActivity(int Y, int M, int D,
			List<Activity_Overview_Model> models) {
		for (int i = 0; i < models.size(); i++) {
			if (Y == BackYear(models.get(i).getStart_time())
					&& M == BackMonth(models.get(i).getStart_time())
					&& D == BackDay(models.get(i).getStart_time())) {
				return getResources().getString(R.string.l_xb40)
						+ panduan(Y, M, D, models)
						+ getResources().getString(R.string.l_xb41);
			}
		}
		return null;
	}

	/**
	 * 判断共有多少个活动
	 * 
	 * @param Y
	 * @param M
	 * @param D
	 * @param models
	 * @return
	 */
	private int panduan(int Y, int M, int D,
			List<Activity_Overview_Model> models) {
		int num = 0;
		Constants.temp_num = new ArrayList<Integer>();
		for (int i = 0; i < models.size(); i++) {
			if (Y == BackYear(models.get(i).getStart_time())
					&& M == BackMonth(models.get(i).getStart_time())
					&& D == BackDay(models.get(i).getStart_time())) {
				Constants.temp_num.add(i);
				++num;
			}
		}
		Constants.temp_map.put(Y + "-" + M + "-" + D, Constants.temp_num);
		// Constants.temp_list.add(Constants.temp_num);
		return num;
	}

	// 将时间中的年份提取出来并转换为int
	public static int BackYear(String time) {
		if (time != null) {
			return Integer.valueOf(time.substring(0, 4));
		} else {
			return 0;
		}

	}

	// 将时间中的月份提取出来并转换为int
	public static int BackMonth(String time) {
		if (time != null) {
			return Integer.valueOf(time.substring(5, 7));
		} else {
			return 0;
		}
	}

	// 将时间中的日提取出来并转换为int
	public static int BackDay(String time) {
		if (time != null) {
			return Integer.valueOf(time.substring(8, time.length()));
		} else {
			return 0;
		}
	}

	private void init(View view) {
		// TODO Auto-generated method stub
		mcalendarTools = CalendarTools.getInstance();
		calendar_temp = Calendar.getInstance();
		tv_Year = (TextView) view.findViewById(R.id.fragment_calendar_year);
		tv_Month = (TextView) view.findViewById(R.id.frament_calendar_month);
		mlist = new ArrayList<Integer>();
		color = new Integer[] {
				// 灰色
				R.drawable.calendar_gridview_click_gray,
				// 深蓝
				R.drawable.calendar_gridview_click_shen_blue,
				// 白色
				R.drawable.calendar_gridview_click_white,
				// 黄色
				R.drawable.calendar_gridview_click_yellow,

		};
		last_month = (Button) view.findViewById(R.id.fragment_last_month);
		next_month = (Button) view.findViewById(R.id.fragment_next_month);
		last_month.setOnClickListener(CalendarFragment.this);
		next_month.setOnClickListener(CalendarFragment.this);
	}

	/**
	 * 初始化数据
	 */
	private void initData(int nian, int yue) {
		// TODO Auto-generated method stub
		if (nian != 0 && yue != 0) {
			year = nian;
			month = yue;
			String mon = mcalendarTools.getNewTime(month);
			tv_Year.setText("" + year);
			tv_Month.setText(mon);
		} else {
			year = Integer.parseInt(mcalendarTools.getNowYear());
			month = Integer.parseInt(mcalendarTools.getNowMonth());
			String mon = mcalendarTools.getNewTime(month);
			tv_Year.setText("" + year);
			tv_Month.setText(mon);
		}
	}

	private void initGridView() {
		// TODO Auto-generated method stub
		if (month != 1 && month != 12) {
			before_month_day = mcalendarTools.getMonthHasHowManeyDay(year,
					month - 1);
			month_day = mcalendarTools.getMonthHasHowManeyDay(year, month);
			after_month_day = mcalendarTools.getMonthHasHowManeyDay(year,
					month + 1);
		} else if (month == 1) {
			before_month_day = mcalendarTools.getMonthHasHowManeyDay(year - 1,
					12);
			month_day = mcalendarTools.getMonthHasHowManeyDay(year, month);
			after_month_day = mcalendarTools.getMonthHasHowManeyDay(year,
					month + 1);
		} else if (month == 12) {
			before_month_day = mcalendarTools.getMonthHasHowManeyDay(year,
					month - 1);
			month_day = mcalendarTools.getMonthHasHowManeyDay(year, month);
			after_month_day = mcalendarTools
					.getMonthHasHowManeyDay(year + 1, 1);
		}
		ViewHolder holder = new ViewHolder(before_month_day, month_day,
				after_month_day);
		// holder.before_month_day=before_month_day;
		// holder.month_day=month_day;
		// holder.after_month_day=after_month_day;

		mGridView = new GridView(getActivity());

		mGridView.setNumColumns(COLUMN_NUMBER);
		mGridView.setColumnWidth(HEIGHT_CHILDHEIGHT);
		mGridView.setScrollbarFadingEnabled(false);
		mGridView.setVerticalScrollBarEnabled(false);
		// 去除GridView的边框
		// mGridView.setSelector(new ColorDrawable(Color.TRANSPARENT));

		calendaradapter = new CalendarGridViewAdapter(holder);
		// mGridView.setDuplicateParentStateEnabled(true);
		mGridView.setAdapter(calendaradapter);
		// mGridView.setOnItemLongClickListener(new OnItemLongClickListener() {
		//
		// @Override
		// public boolean onItemLongClick(AdapterView<?> arg0, View arg1,
		// int arg2, long arg3) {
		// // TODO Auto-generated method stub
		// Toast.makeText(getActivity(), ""+arg2, 0).show();
		// return true;
		// }
		// });
//		   
		mGridView.setOnItemClickListener(new OnItemClickListener() {

			@Override
			public void onItemClick(AdapterView<?> arg0, View view,
					int position, long arg3) {
				// TODO Auto-generated method stub

				if (calendarGridViewListener != null) {
					calendarGridViewListener.ItemListener(arg0, view, position,
							arg3, calendaradapter);
				}
				//    
				// Toast.makeText(getActivity(),
				// ""+calendaradapter.getItem(position), 0).show();
			}
		});
//		   
		import_layout.addView(mGridView);
	}

	@Override
	public void onPause() {
		// TODO Auto-generated method stub
		super.onPause();
	}

	// private class MyPagerAdapter extends PagerAdapter

	public class ViewHolder {
		int before_month_day;
		int month_day;
		int after_month_day;

		public ViewHolder(int before_month_day, int month_day,
				int after_month_day) {
			this.before_month_day = before_month_day;
			this.month_day = month_day;
			this.after_month_day = after_month_day;
		}

	}

	/**
	 * 具体思路是这样的，将本月，下月，上月的天数传过来，判断本月的一号是星期几 ，然后将它前面的填充为上月的天数，下月的似乎不用传过来也可以
	 * 
	 * @author Administrator
	 * 
	 */
	public class CalendarGridViewAdapter extends BaseAdapter {

		private List<Integer> mlist;
		private int before_month_day;
		private int month_day;
		private int after_month_day;
		// 记录三个阶段的数量
		private int yijieNumber = 0;
		private int erjieNumber = 0;
		private int sanjieNumber = 0;
		/**
		 * 当前月的第一天是星期几
		 */
		private int month_day0;

		public CalendarGridViewAdapter(ViewHolder vHolder) {
			before_month_day = vHolder.before_month_day;
			month_day = vHolder.month_day;
			after_month_day = vHolder.after_month_day;

			mlist = new ArrayList<Integer>();
			month_day0 = mcalendarTools.getCaculateWeekDay(year, month, 1);

			// 如果是星期4，那么前面三格将填充上个月的;
			for (int i = (before_month_day - month_day0) + 1; i <= before_month_day; i++) {
				mlist.add(i);
				yijieNumber++;
			}
			for (int i = 1; i <= month_day; i++) {
				mlist.add(i);
				erjieNumber++;
			}
			int mlistSize = mlist.size();
			for (int i = 1; i <= (zonggeshu - mlistSize); i++) {
				//    
				mlist.add(i);
				sanjieNumber++;
			}

		}

		@Override
		public int getCount() {
			// TODO Auto-generated method stub
			return mlist.size();
		}

		@Override
		public Object getItem(int position) {
			// TODO Auto-generated method stub
			return mlist.get(position);
		}

		// 获取选中项的年月日
		public String getYearMonthDay(int position) {
			String str = "";
			String year = tv_Year.getText().toString();
			String month = tv_Month.getText().toString();
			String day = "" + getItem(position);
			if (position >= yijieNumber
					&& position < (yijieNumber + erjieNumber)) {
				if (Integer.parseInt(day) < 10) {
					day = "0" + day;
				}

				str = year + "-" + month + "-" + day;
			}
			return str;

		}

		@Override
		public long getItemId(int position) {
			// TODO Auto-generated method stub
			return position;
		}

		@Override
		public View getView(final int position, View convertView,
				ViewGroup parent) {
			// TODO Auto-generated method stub
			// ll是整个gridView的每个item子类
			LinearLayout ll = new LinearLayout(getActivity());
			ll.setOrientation(LinearLayout.VERTICAL);
			// ll.setClickable(true);
			// ll0是item上面的小部分
			LinearLayout ll0 = new LinearLayout(getActivity());
			ll0.setOrientation(LinearLayout.HORIZONTAL);
			// tv是显示日期
			TextView tv = new TextView(getActivity());
			tv.setTextSize(10);
			tv.setText("" + mlist.get(position));
			if (mlist.get(position) < 10) {
				tv.setText("" + mlist.get(position) + "  ");
			}
			Constants.START_TIME_default = tv_Year.getText().toString() + "-"
					+ tv_Month.getText().toString() + "-01";
			Constants.END_TIME_default = tv_Year.getText().toString() + "-"
					+ tv_Month.getText().toString() + "-" + erjieNumber;
//			   (Constants.START_TIME_default + " "
//					+ Constants.END_TIME_default);
			// 在这里判定设置什么背景色
			if (position >= 0 && position < yijieNumber) {
				ll.setBackgroundResource(color[0]);
			} else if (position >= yijieNumber
					&& position < (yijieNumber + erjieNumber)) {
				//    ("一节:" + yijieNumber + " " + "二节:"
				// + erjieNumber);
				ll.setBackgroundResource(color[2]);
				// ll.setOnClickListener(new OnClickListener() {
				//
				// @Override
				// public void onClick(View arg0) {
				//
				// if (backdd(
				// Constants.temp_map,
				// tv_Year.getText().toString() + "-"
				// + tv_Month.getText().toString() + "-"
				// + mlist.get(position)).size() > 0) {
				// Intent intent = new Intent(getActivity(),
				// Data_During_Activity.class);
				// intent.setFlags(Intent.FLAG_ACTIVITY_NEW_TASK);
				// intent.putExtra("日期", tv_Year.getText().toString()
				// + "." + tv_Month.getText().toString() + "."
				// + mlist.get(position));
				// startActivity(intent);
				// } else {
				//
				// }
				// }
				// });
			} else if (position >= (yijieNumber + erjieNumber)
					&& position < (yijieNumber + erjieNumber + sanjieNumber)) {
				ll.setBackgroundResource(color[0]);
			}
			int todayIsDay_Of_Month = Integer.parseInt(mcalendarTools
					.getToDayIs_DayOfMonth());

			// 这是确定哪天今天，并且改背景为黄色
			if (Integer.parseInt(tv_Year.getText().toString()) == Integer
					.parseInt(mcalendarTools.getNowYear())
					&& Integer.parseInt(tv_Month.getText().toString()) == Integer
							.parseInt(mcalendarTools.getNowMonth())
					&& position == todayIsDay_Of_Month + yijieNumber - 1) {
				ll.setBackgroundResource(color[3]);
			}
			dip5_px = DisplayUtil.dip2px(getActivity(), 5);
			if (position >= yijieNumber
					&& position < (yijieNumber + erjieNumber)) {
				tv.setTextColor(getActivity().getResources().getColor(
						R.color.blue));
			}
			tv.setPadding(dip5_px, dip5_px, 0, 0);
			ll0.addView(tv);

//			   

			// ll1是下面的视图
			LinearLayout ll1 = new LinearLayout(getActivity());
			ll1.setOrientation(LinearLayout.HORIZONTAL);

			if (calendarListener != null && position >= yijieNumber
					&& position < (yijieNumber + erjieNumber)) {
				// 开始活动的提示（）
				if (calendarListener.AddStartButton(
						Integer.parseInt(tv_Year.getText().toString().trim()),
						Integer.parseInt(tv_Month.getText().toString().trim()),
						mlist.get(position))) {
					// 在这里添加开始
					ll0.addView(AddStartTitle());
				}
				// 添加结束的提示
				if (calendarListener.AddEndButton(
						Integer.parseInt(tv_Year.getText().toString().trim()),
						Integer.parseInt(tv_Month.getText().toString().trim()),
						mlist.get(position))) {
					ll0.addView(AddEndTitle());
				}
				// 添加活动的内容
				if (!StringUtil.isNullOrEmpty(calendarListener.AddActivity(
						Integer.parseInt(tv_Year.getText().toString().trim()),
						Integer.parseInt(tv_Month.getText().toString().trim()),
						mlist.get(position)))) {
					TextView lvcheng = new TextView(getActivity());
					// 在这里设置有哪些活动
					lvcheng.setText(calendarListener.AddActivity(
							Integer.parseInt(tv_Year.getText().toString()
									.trim()),
							Integer.parseInt(tv_Month.getText().toString()
									.trim()), mlist.get(position)));
					String key = Integer.parseInt(tv_Year.getText().toString()
							.trim())
							+ "-"
							+ Integer.parseInt(tv_Month.getText().toString()
									.trim()) + "-" + mlist.get(position);
					map.put(key, lvcheng.getText().toString());

					int sp6_px = DisplayUtil.sp2px(getActivity(), 6);
					// lvcheng是活动显示的textView
					lvcheng.setTextSize(sp6_px);
					lvcheng.setPadding(dip5_px, 0, dip5_px, 0);
					lvcheng.setTextColor(getActivity().getResources().getColor(
							R.color.f_white));
					ll.setBackgroundResource(color[1]);
					ll1.addView(lvcheng);
				}
				//
			}

			int dip50_px = DisplayUtil.dip2px(getActivity(), 50);
			LinearLayout.LayoutParams vl = new LinearLayout.LayoutParams(
					LayoutParams.MATCH_PARENT, dip50_px);
			ll1.setLayoutParams(vl);

			ll.addView(ll0);
			ll.addView(ll1);

			return ll;
		}

		/**
		 * 匹配当前时间是否有活动
		 * 
		 * @param map匹配的map
		 * @param time传入需要匹配的时间
		 */
		public List<Integer> backdd(Map<String, Object> map, String time) {
			Set<Map.Entry<String, Object>> set = map.entrySet();
			// boolean jud = false;
			List<Integer> integers = new ArrayList<Integer>();
			for (Iterator<Map.Entry<String, Object>> it = set.iterator(); it
					.hasNext();) {
				Map.Entry<String, Object> entry = (Map.Entry<String, Object>) it
						.next();
				//    (entry.getKey() + "--->" +
				// entry.getValue());
				if (Utils.getStringMillis(time) == Utils.getStringMillis(entry
						.getKey())) {
					// jud = true;
					integers = (List<Integer>) entry.getValue();
				}
			}
			Constants.temp_num = integers;
			return integers;
		}

		/**
		 * 添加一个开始的提示
		 * 
		 * @return
		 */
		public Button AddStartTitle() {
			Button start = new Button(getActivity());
			start.setFocusable(false);
			start.setTextSize(5);
			int dip20_px = DisplayUtil.dip2px(getActivity(), 15);
			LinearLayout.LayoutParams ml = new LinearLayout.LayoutParams(
					LayoutParams.MATCH_PARENT, dip20_px);
			start.setLayoutParams(ml);
			start.setTextColor(getResources().getColor(R.color.f_white));
			start.setBackgroundResource(R.drawable.start_time);
			return start;
		}

		/**
		 * 添加一个结束的提示
		 * 
		 * @return
		 */
		public Button AddEndTitle() {
			Button start = new Button(getActivity());
			start.setFocusable(false);
			start.setTextSize(5);
			int dip20_px = DisplayUtil.dip2px(getActivity(), 15);
			LinearLayout.LayoutParams ml = new LinearLayout.LayoutParams(
					LayoutParams.MATCH_PARENT, dip20_px);
			start.setLayoutParams(ml);
			start.setTextColor(getResources().getColor(R.color.f_white));
			start.setBackgroundResource(R.drawable.over_time);
			return start;
		}

		public List<Integer> getMlist() {
			return mlist;
		}

		public void setMlist(List<Integer> mlist) {
			this.mlist = mlist;
		}

		public int getYijieNumber() {
			return yijieNumber;
		}

		public void setYijieNumber(int yijieNumber) {
			this.yijieNumber = yijieNumber;
		}

		public int getErjieNumber() {
			return erjieNumber;
		}

		public void setErjieNumber(int erjieNumber) {
			this.erjieNumber = erjieNumber;
		}

		public int getSanjieNumber() {
			return sanjieNumber;
		}

		public void setSanjieNumber(int sanjieNumber) {
			this.sanjieNumber = sanjieNumber;
		}

	}

	private void li_activity_show(final String start_ti, final String end_ti) {
		Utils.showPrgress(getActivity());
		MyThreadTool.fixedThreadPool.execute(new Runnable() {
			@Override
			public void run() {
				models.clear();
				models_1.clear();
				Map<String, Object> map = new HashMap<String, Object>();
				map.put("start", start_ti);
				map.put("end", end_ti);

				if (cid != null) {
					map.put("cid", cid);
					map.put("uid", "0");
				} else {
					map.put("uid", uid);
					map.put("cid", "0");
				}
				map.put("fid", Utils.getUid(getActivity()));
				map.put("languageType", com.badibadi.mytools.Dialog
						.getSystemLanguageTypegrzx(getActivity()));
				String t_content = Utils.sendRequest(map, Constants.BadiUrl
						+ "li_activity_show");
				if (t_content != null) {
					//    ("返回期间的活动是:" + t_content);
					results = Utils.checkResult_NNN(getActivity(), t_content);
					if (results != null && !results.getRetmsg().equals("null")) {
						try {
							overview_Models_starttt = JSONUtils
									.getListByJsonString(results.getRetmsg(),
											Activity_Overview_Model.class);
							handler.sendEmptyMessage(3);
						} catch (JSONException e) {
							// TODO Auto-generated catch block
							   
						}
					} else {
						handler.sendEmptyMessage(3);
					}
				} else {
					handler.sendEmptyMessage(3);
				}
			}
		});
	}

	@Override
	public void onClick(View v) {
		// TODO Auto-generated method stub
		switch (v.getId()) {
		case R.id.fragment_last_month:
			UpPage();
			--temp_num;
			if (is_betweenActivity) {// 是期间内的活动
//				   
			} else {// 不是期间内的活动
				try {
					li_activity_show(Utils.getFirstday_Month(Get___Data(Utils
							.GetSystemCurrentTime1(calendar_temp)), temp_num),
							Utils.getLastday_Month(Get___Data(Utils
									.GetSystemCurrentTime1(calendar_temp)),
									temp_num));
				} catch (ParseException e) {
					// TODO Auto-generated catch block
					   
				}

			}
			break;
		case R.id.fragment_next_month:
			DownPage();
			++temp_num;
			if (is_betweenActivity) {// 是期间内的活动
//				   
			} else {// 不是期间内的活动
				try {
					li_activity_show(Utils.getFirstday_Month(Get___Data(Utils
							.GetSystemCurrentTime1(calendar_temp)), temp_num),
							Utils.getLastday_Month(Get___Data(Utils
									.GetSystemCurrentTime1(calendar_temp)),
									temp_num));
				} catch (ParseException e) {
					// TODO Auto-generated catch block
					   
				}
			}
			break;
		}
	}

	/**
	 * 获取当前时间日期的Data
	 * 
	 * @param timetime
	 * @return
	 * @throws ParseException
	 */
	private Date Get___Data(String timetime) throws ParseException {
		SimpleDateFormat df = new SimpleDateFormat("yyyy-MM-dd");
		Date date;
		date = df.parse(timetime);
		return date;
	}

	// 上翻
	public void UpPage() {
		if (!map.isEmpty()) {
			map.clear();
		}
		if (!Constants.temp_map.isEmpty()) {
			Constants.temp_map.clear();
		}
		// 不是第一月的话
		if (Integer.parseInt(tv_Month.getText().toString()) != 1) {
			if (Integer.parseInt(tv_Month.getText().toString()) < 11) {
				tv_Month.setText("0"
						+ (Integer.parseInt(tv_Month.getText().toString()) - 1));

			} else {
				tv_Month.setText(""
						+ (Integer.parseInt(tv_Month.getText().toString()) - 1));
			}
		} else {
			tv_Month.setText("12");
			tv_Year.setText(""
					+ (Integer.parseInt(tv_Year.getText().toString()) - 1));
		}
		notifyCalendar(Integer.parseInt(tv_Year.getText().toString()),
				Integer.parseInt(tv_Month.getText().toString()));
	}

	/**
	 * 更新日历
	 * 
	 * @param Year
	 * @param Month
	 */
	private void notifyCalendar(int Year, int Month) {
		int yijie = 0;
		int erjie = 0;
		int sanjie = 0;

		if (Month != 1 && Month != 12) {
			before_month_day = mcalendarTools.getMonthHasHowManeyDay(Year,
					Month - 1);
			month_day = mcalendarTools.getMonthHasHowManeyDay(Year, Month);
			after_month_day = mcalendarTools.getMonthHasHowManeyDay(Year,
					Month + 1);
		} else if (Month == 1) {
			before_month_day = mcalendarTools.getMonthHasHowManeyDay(Year - 1,
					12);
//			   
			month_day = mcalendarTools.getMonthHasHowManeyDay(Year, Month);
			after_month_day = mcalendarTools.getMonthHasHowManeyDay(Year,
					Month + 1);
		} else if (Month == 12) {
			before_month_day = mcalendarTools.getMonthHasHowManeyDay(Year,
					Month - 1);
			month_day = mcalendarTools.getMonthHasHowManeyDay(Year, Month);
			after_month_day = mcalendarTools
					.getMonthHasHowManeyDay(Year + 1, 1);
		}
		List<Integer> mylist = new ArrayList<Integer>();
		int month_day0 = mcalendarTools.getCaculateWeekDay(Year, Month, 1);
		for (int i = (before_month_day - month_day0) + 1; i <= before_month_day; i++) {
			mylist.add(i);
			yijie++;
//			   
		}
		for (int i = 1; i <= month_day; i++) {
			mylist.add(i);
			erjie++;
		}
		int mlistSize = mylist.size();
		for (int i = 1; i <= (zonggeshu - mlistSize); i++) {
			//    
			mylist.add(i);
			sanjie++;
		}
		calendaradapter.setMlist(mylist);
		calendaradapter.setYijieNumber(yijie);
		calendaradapter.setErjieNumber(erjie);
		calendaradapter.setSanjieNumber(sanjie);
		calendaradapter.notifyDataSetInvalidated();

	}

	/** 点击下一页后下翻 */
	public void DownPage() {
		if (!map.isEmpty()) {
			map.clear();
		}
		if (!Constants.temp_map.isEmpty()) {
			Constants.temp_map.clear();
		}
		if (Integer.parseInt(tv_Month.getText().toString()) != 12) {
			if (Integer.parseInt(tv_Month.getText().toString()) < 9) {
				tv_Month.setText("0"
						+ (Integer.parseInt(tv_Month.getText().toString()) + 1));
			} else {
				tv_Month.setText(""
						+ (Integer.parseInt(tv_Month.getText().toString()) + 1));
			}
		} else {
			tv_Month.setText("01");
			tv_Year.setText(""
					+ (Integer.parseInt(tv_Year.getText().toString()) + 1));
		}
		notifyCalendar(Integer.parseInt(tv_Year.getText().toString()),
				Integer.parseInt(tv_Month.getText().toString()));
	}

	public boolean isIs_betweenActivity() {
		return is_betweenActivity;
	}

	public void setIs_betweenActivity(boolean is_betweenActivity) {
		this.is_betweenActivity = is_betweenActivity;
	}

	public static CalendarFragment getInstance() {
		if (calendarFragment == null) {
			return new CalendarFragment();
		}
		return calendarFragment;
	}

	public void setOnCalendarOnClistener(CalendarListener cListener) {
		calendarListener = cListener;
	}

	public void setOnCalendarItemClistener(CalendarGridViewListener clistener) {
		calendarGridViewListener = clistener;
	}

	public void getMyMapItemClistener(CalendarFragment.getHashMap hashMap) {
		hashMap.getMap(map);
	}

	public interface CalendarListener {
		boolean AddStartButton(int Year, int Month, int Day);

		boolean AddEndButton(int Year, int Month, int Day);

		String AddActivity(int Year, int Month, int Day);
	}

	public interface CalendarGridViewListener {
		void ItemListener(AdapterView<?> arg0, View view, int position,
				long arg3, CalendarGridViewAdapter calendaradapter);
	}

	public interface getHashMap {
		void getMap(Map<String, Object> map);
	}// CalendarGridViewAdapter

	public void getCalendarAdapter(getAdapter adapter) {
		adapter.getAdapter(calendaradapter);
	}

	public interface getAdapter {
		void getAdapter(CalendarGridViewAdapter adapter);
	}
}
